package com.xdemo.personal.fileupload;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class SingleDirectoryStore implements IFileStore {
	
	private File directory;
	
	public SingleDirectoryStore(String directory) {
		this.directory = new File(directory);
		if(!this.directory.exists()) {
			this.directory.mkdir();
		}
	}

	@Override
	public File getFreshFileToStore(String fileType) {
		String _name = UUID.randomUUID().toString();
		File _file = new File(directory,_name + fileType);
		if(_file.exists()) {
			return getFreshFileToStore(fileType);
		}
		try {
			_file.createNewFile();
			return _file;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
